/**
 * Created with IntelliJ IDEA.
 * Description:
 * user:likai
 * Date:2022-11-13
 * Time:12:37
 */
class ListNode{
    int val;
    ListNode next;
    public ListNode(int val){
        this.val=val;
    }
}
public class Test {
    public ListNode reverseList(ListNode head) {
        if(head==null){
            return null;
        }
        ListNode cur=head.next;
        head.next=null;
        while(cur!=null){
            ListNode curnext=cur.next;
            cur.next=head;
            head=cur;
            cur=curnext;
        }
        return head;
    }
    public class Solution {
        public ListNode detectCycle(ListNode head) {
            if(head==null){
                return null;
            }
            ListNode fast=head;
            ListNode slow=head;
            while(fast.next!=null&&fast.next.next!=null){
                fast=fast.next.next;
                slow=slow.next;
                if(fast==slow){
                    fast=head;
                    while(fast!=slow){
                        fast=fast.next;
                        slow=slow.next;
                    }
                    return fast;
                }
            }
            return null;
        }
    }
    public static boolean chkPalindrome(ListNode A) {
        // write code here
        if(A==null||A.next==null){
            return true;
        }
        ListNode fast=A;
        ListNode slow=A;
        while(fast.next!=null&&fast.next.next!=null){
            fast=fast.next.next;
            slow=slow.next;
        }
        //反转后半部分链表
        ListNode cur=slow.next;
        slow.next=null;
        ListNode curnext=cur.next;
        cur.next=null;
        while(curnext!=null){
            ListNode pre=curnext.next;
            curnext.next=cur;
            cur=curnext;
            curnext=pre;
        }
        //循环判断
        while(cur!=null){
            if(cur.val!=A.val){
                return false;
            }
            cur=cur.next;
            A=A.next;
        }
        return true;
    }

    public static void main(String[] args) {
        ListNode l1=new ListNode(2);
        ListNode l2=new ListNode(3);
        ListNode l3=new ListNode(2);
        ListNode l4=new ListNode(3);
        ListNode l5=new ListNode(2);
        l1.next=l2;
        l2.next=l3;
        l3.next=l4;
        l4.next=l5;
        System.out.println(chkPalindrome(l1));
    }
}
